package frame.project;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Toast;


/**
 * Import images from SD card.
 * 
 * @author Jourdan
 */
public class Imports extends Activity {    
	ArrayList<String> alUri = new ArrayList<String>();
	
    /**
     * Creates the adapter and the click listener.
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.imports);

        loadImages();
    }

    /**
     * Load images.
     */
    private void loadImages() {
        final Object data = getLastNonConfigurationInstance();
        if (data == null) {
            new LoadImagesFromSDCard().execute();
        } else {
           
        }
    }
    
    /**
     * Async task for loading the images from the SD card. 
     * 
     * @author Jourdan
     *
     */
    class LoadImagesFromSDCard extends AsyncTask<Object, String, Object> {
        
        /**
         * Load images from SD Card in the background
         *  
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            //setProgressBarIndeterminateVisibility(true); 
            Uri uri = null;	

            // Set up an array of the Title Image ID column we want
            String[] mProjection = {MediaStore.Images.ImageColumns._ID};
            // Create the cursor pointing to the SDCard
            Cursor cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    mProjection, // Which columns to return
                    null,       // Return all rows
                    null,       
                    null); 
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID);
            int size = cursor.getCount();
            // If size is 0, there are no images on the SD Card.
            if (size == 0) {
                //No Images available, post some message to the user
            }
            int imageID = 0;
            for (int i = 0; i < size; i++) {
                cursor.moveToPosition(i);
                imageID = cursor.getInt(columnIndex);
                uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + imageID);
                publishProgress(uri.toString());
            }
            //Toast.makeText(getApplicationContext(), Integer.toString(size), Toast.LENGTH_SHORT).show();
            cursor.close();
            return null;
        }
        /**
         * Add a new LoadedImage in the images grid.
         *
         * @param value The image.
         */
        @Override
        public void onProgressUpdate(String... value) {
        	alUri.add(value[value.length-1]);
        }
        /**
         * Set the visibility of the progress bar to false.
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
        	Toast.makeText(getApplicationContext(), Integer.toString(alUri.size()), Toast.LENGTH_SHORT).show();
        	
        	//setProgressBarIndeterminateVisibility(false);
        }
    }
}